﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyBanLe.Admin;
using QuanLyBanLe.Sell;
using QuanLyBanLe.Benefit;
using DLL;
using BLL;
using Helper;
using QuanLyBanLe.Constants;
using Logging;
using System.Configuration;
using System.ServiceProcess;
using QuanLyBanLe.Depository;

namespace QuanLyBanLe
{
    public partial class FormMain : Form
    {
        public static Setting s_Setting;
        public static User s_User;
        public static string s_OS;

        public FormMain()
        {
            InitializeComponent();
            StartServices();
        }

        private void StartServices()
        {
            // Check MSSQLSERVER is started yet, if not start it
            string sqlService = ConfigurationManager.AppSettings["SQLService"];
            if (string.IsNullOrEmpty(sqlService))
            {
                sqlService = "MSSQLSERVER";
            }
            ServiceController service = new ServiceController(sqlService);
            try
            {
                TimeSpan timeout = TimeSpan.FromMilliseconds(5000);
                if (service.Status == ServiceControllerStatus.Stopped)
                {
                    service.Start();
                    service.WaitForStatus(ServiceControllerStatus.Running, timeout);
                    MyLogger.LogInfo("MSSQLSERVER Started");
                }
            }
            catch (Exception ex)
            {
                MyLogger.LogException("Lỗi xảy ra khi start MSSQLSERVER service", ex);
            }
        }

        private void mniLogin_Click(object sender, EventArgs e)
        {
            Logon();
        }

        private void mniLogout_Click(object sender, EventArgs e)
        {
            // Do logout
            HideMenu();
            s_User = null;
            mniLogin.Visible = true;
        }

        private void mniExit_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }

        private void mniLoaiHang_Click(object sender, EventArgs e)
        {
            FormProductType frm = new FormProductType();
            frm.Show();
        }

        private void mniNhaSanXuat_Click(object sender, EventArgs e)
        {
            FormProducer frm = new FormProducer();
            frm.Show();
        }


        private void mniProduct_Click(object sender, EventArgs e)
        {
            FormProduct frm = new FormProduct();
            frm.Show();
        }

        private void mniNhapKho_Click(object sender, EventArgs e)
        {
            FormInProdPaper frm = new FormInProdPaper();
            frm.Show();
        }

        private void mniInDepositotyHistory_Click(object sender, EventArgs e)
        {
            FormInProductHistory frm = new FormInProductHistory();
            frm.Show();
        }

        private void mniXuatKho_Click(object sender, EventArgs e)
        {
            FormSelectOutProdPaperType frm = new FormSelectOutProdPaperType();
            frm.Show();
        }

        private void mniOutDepositoryHistory_Click(object sender, EventArgs e)
        {
            FormOutProductHistory frm = new FormOutProductHistory();
            frm.Show();
        }

        private void mniThongKeHangTon_Click(object sender, EventArgs e)
        {
            FormProductStatistics frm = new FormProductStatistics();
            frm.Show();
        }

        private void mniGenerateBarcode_Click(object sender, EventArgs e)
        {
            FormGenerateBarcode frm = new FormGenerateBarcode();
            frm.Show();
        }

        private void mniQuanLyHoaDon_Click(object sender, EventArgs e)
        {
            FormOrder frm = new FormOrder();
            frm.ShowDialog();
        }

        private void ThongKeHoaDon_Click(object sender, EventArgs e)
        {
            FormBillHistory frm = new FormBillHistory();
            frm.Show();
        }

        private void mniReturnProductHistory_Click(object sender, EventArgs e)
        {
            FormReturnProductHistory frm = new FormReturnProductHistory();
            frm.Show();
        }

        private void mniBenefitStat_Click(object sender, EventArgs e)
        {
            FormSellStatistics frm = new FormSellStatistics();
            frm.Show();
        }

        private void mniQuanLyNguoiDung_Click(object sender, EventArgs e)
        {
            FormUserMgr frm = new FormUserMgr();
            frm.Show();
        }

        private void mniDoiMatKhau_Click(object sender, EventArgs e)
        {
            FormChangePassword frm = new FormChangePassword();
            frm.Show();
        }

        private void mniCaiDatThongTin_Click(object sender, EventArgs e)
        {
            FormSetting frm = new FormSetting();
            frm.Show();
        }

        private void mniChangeProtectPass_Click(object sender, EventArgs e)
        {
            FormChangeProtectPass frm = new FormChangeProtectPass();
            frm.Show();
        }

        private void mniResetInfo_Click(object sender, EventArgs e)
        {
            FormResetSale frm = new FormResetSale();
            frm.Show();
        }

        private void mniResetProductAmount_Click(object sender, EventArgs e)
        {
            FormResetProductAmount frm = new FormResetProductAmount();
            frm.Show();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            // Hidden menu
            HideMenu();

            //TODO: authentication & authorization
            SettingService settingService = new SettingService();
            s_Setting = settingService.Get();

            string mode = ConfigurationManager.AppSettings["Mode"];
            if (mode != "Client")
            {
                // check activation code
                string key = System.Environment.MachineName;
                string code = Encrypt.Hash(key);
                if (code != s_Setting.ActivationCode)
                {
                    MessageBox.Show("Mã kích hoạt không hợp lệ, vui lòng liên hệ tác giả qua emai: trongdauanh@gmail.com hoặc phone: 0936087214 để nhận mã kích hoạt, cám ơn.",
                        "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.Close();
                    Application.Exit();
                }
                else
                {
                    Logon();
                }
            }
            else
            {
                Logon();
            }
            this.Text += GetShopInfo();
        }

        public static string GetShopInfo() 
        {
            return " - " + s_Setting.ShopNamePrefix + " " + s_Setting.ShopName + " - " + s_Setting.ShopAddress;
        }

        private void Logon()
        {
            FormLogin frm = new FormLogin();
            frm.ShowDialog();

            if (frm.LogonSuccess)
            {
                s_User = frm.User;
                Authorize();
                s_OS = ConfigurationManager.AppSettings["OS"];
                if (string.IsNullOrEmpty(s_OS))
                {
                    s_OS = "WinXP";
                }
            }
        }

        private void HideMenu()
        {
            mniThongTin.Visible = false;
            mniKho.Visible = false;
            mniBanHang.Visible = false;
            mniTaiChinh.Visible = false;
            mniNguoiDung.Visible = false;
            mniCaiDat.Visible = false;
            mniQuanLyNguoiDung.Visible = false;
            mniThongKeHangTon.Visible = false;
            mniLogout.Visible = false;
        }

        private void Authorize()
        {
            mniLogin.Visible = false;
            mniLogout.Visible = true;
            if (s_User.Role == Roles.Admin)
            {
                mniThongTin.Visible = true;
                mniKho.Visible = true;
                mniBanHang.Visible = true;
                mniTaiChinh.Visible = true;
                mniNguoiDung.Visible = true;
                mniReturnProductHistory.Visible = true;
                ThongKeHoaDon.Visible = true;
                mniCaiDat.Visible = true;
                mniQuanLyNguoiDung.Visible = true;
                mniThongKeHangTon.Visible = true;
                mniNhapKho.Visible = true;
                mniXuatKho.Visible = true;
                mniInDepositotyHistory.Visible = true;
                mniOutDepositoryHistory.Visible = true;
                mniThongKeHangTon.Visible = true;
                mniGenerateBarcode.Visible = true;
            }
            else if (s_User.Role == Roles.Manager)
            {
                mniKho.Visible = true;
                mniBanHang.Visible = true;
                mniNguoiDung.Visible = true;
                mniReturnProductHistory.Visible = true;
                ThongKeHoaDon.Visible = true;
                mniKho.Visible = true;
                mniNhapKho.Visible = true;
                mniXuatKho.Visible = true;
                mniInDepositotyHistory.Visible = true;
                mniOutDepositoryHistory.Visible = true;
                mniThongKeHangTon.Visible = true;
                mniGenerateBarcode.Visible = true;
            }
            else if (s_User.Role == Roles.Depository)
            {
                mniKho.Visible = true;
                mniNguoiDung.Visible = true;
                mniKho.Visible = true;
                mniNhapKho.Visible = true;
                mniXuatKho.Visible = true;
                mniInDepositotyHistory.Visible = true;
                mniOutDepositoryHistory.Visible = true;
                mniThongKeHangTon.Visible = true;
                mniGenerateBarcode.Visible = true;
            }
            else if (s_User.Role == Roles.Sale)
            {
                mniBanHang.Visible = true;
                mniNguoiDung.Visible = true;
                mniReturnProductHistory.Visible = true;
                ThongKeHoaDon.Visible = false;
                mniKho.Visible = true;
                mniNhapKho.Visible = false;
                mniXuatKho.Visible = false;
                mniInDepositotyHistory.Visible = false;
                mniOutDepositoryHistory.Visible = false;
                mniThongKeHangTon.Visible = false;
                mniGenerateBarcode.Visible = true;
            }
        }

        private void mniProductInfoReport_Click(object sender, EventArgs e)
        {
            FormProductInfoReport frm = new FormProductInfoReport();
            frm.ShowDialog();
        }
    }
}
